<template>
  <el-dropdown trigger="hover">
    <div class="avatar">
      <img src="@/assets/images/avatar.png" alt="avatar" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="$router.push('/user')">
          <el-icon><i-ep-user /></el-icon>个人信息
        </el-dropdown-item>
        <el-dropdown-item @click="onPwd">
          <el-icon><i-ep-edit /></el-icon>修改密码
        </el-dropdown-item>
        <el-dropdown-item @click="logout" divided>
          <el-icon><i-ep-switch-button /></el-icon>登出
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <ChangePassword v-model:edit-visible="pwdVisible" :set-edit-visible="setPwdVisible" :create-data="createData" />
</template>

<script setup lang="ts">
import { set } from '@vueuse/core'
import { useRouter } from 'vue-router'
import useUserStore from '@/store/modules/user'
import ChangePassword from './ChangePassword.vue'

const userStore = useUserStore()
const router = useRouter()
const pwdVisible = ref(false)

const setPwdVisible = (v = false) => {
  set(pwdVisible, v)
}

// 修改密码
const onPwd = () => {
  setPwdVisible(true)
}

// 退出
const logout = async () => {
  await userStore.logout()

  router.push('/login')
}

const createData = () => {}
</script>

<style scoped lang="scss">
.avatar {
  width: 40px;
  height: 40px;
  overflow: hidden;
  cursor: pointer;
  border-radius: 50%;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>
